package com.hr;

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Transient;

import org.hibernate.annotations.BatchSize;
import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;

import com.espirit.eap.ICheckModelDuplicateInfo;
import com.espirit.eap.annotation.DomainModel;
import com.espirit.eap.manager.Department;
import com.espirit.eap.sa.DepartmentAware;
import com.espirit.finance.core.AcctTerm;
import com.espirit.finance.core.AcctTermCheckAware;
import com.googlecode.cswish.model.Saas;

@Entity
@DomainModel
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
@BatchSize(size=10)
public class SalaryDefine implements DepartmentAware, Serializable,ICheckModelDuplicateInfo, AcctTermCheckAware, Saas {

	private static final long serialVersionUID = -492320413956492769L;
	
	private Integer sid;
	public Integer getSid() {
		return sid;
	}

	public void setSid(Integer sid) {
		this.sid = sid;
	}

	private Integer id;
	
	@Id
	@GeneratedValue(strategy=GenerationType.IDENTITY)
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	
	@ManyToOne(fetch=FetchType.LAZY)
	public Department getDepartment() {
		return department;
	}
	public void setDepartment(Department department) {
		this.department = department;
	}
	
	public BigDecimal getMonthSalaryDays() {
		return monthSalaryDays;
	}
	public void setMonthSalaryDays(BigDecimal monthSalaryDays) {
		this.monthSalaryDays = monthSalaryDays;
	}
	public BigDecimal getDaySalaryHours() {
		return daySalaryHours;
	}
	public void setDaySalaryHours(BigDecimal daySalaryHours) {
		this.daySalaryHours = daySalaryHours;
	}
	
	@ManyToOne(fetch=FetchType.LAZY)
	public AcctTerm getAcctTerm() {
		return acctTerm;
	}
	public void setAcctTerm(AcctTerm acctTerm) {
		this.acctTerm = acctTerm;
	}

	@Transient
	public String getDeptIds() {
		return deptIds;
	}
	public void setDeptIds(String deptIds) {
		this.deptIds = deptIds;
	}
	
	private Department department;
	private String deptIds;
	
	private BigDecimal monthSalaryDays;
	private BigDecimal daySalaryHours;
	private List<SalaryEmployeeDefine> employeeDefines;
	private AcctTerm acctTerm;
	

	private static final String[] properties = new String[]{"department+acctTerm"};
	@Override
	public String[] gatherProperties() {
		return properties;
	}
	
	@OneToMany(targetEntity = SalaryEmployeeDefine.class ,orphanRemoval = true, fetch=FetchType.LAZY,  cascade = CascadeType.ALL, mappedBy = "parent")
	public List<SalaryEmployeeDefine> getEmployeeDefines() {
		return employeeDefines;
	}
	public void setEmployeeDefines(List<SalaryEmployeeDefine> employeeDefines) {
		this.employeeDefines = employeeDefines;
	}
	
	
	@Override
	public boolean check(AcctTerm acctTerm) {
		if (acctTerm != null && acctTerm.getBeginDate() != null && this.acctTerm != null){
			return acctTerm.getName().compareTo(this.acctTerm.getName()) <= 0;
		} else {
			return true;
		}
	}
	
}
